var audioMgr = cc.Class({
    properties:{
        btnCheck:null,
        gameWin:null,
        gameLose:null,
        qiuPass:null,
        qiuCollider:null,
        time:0,
        gameMusic:null,
        gameMusicId:null,
    },

    //按钮音效
    playButtonAudio:function(){
        if(window.gameConfig.isOpenSound != "1")return;
        var self = this;
        if(this.btnCheck == null)
        {
            this.onLodeAudioRes("btnCheck",(audio)=>{
                self.btnCheck = audio;
                cc.audioEngine.play(self.btnCheck);
            });
        }
        else cc.audioEngine.play(this.btnCheck);
    },

    //游戏胜利音效
    playGameWinAudio:function(){
        if(window.gameConfig.isOpenSound != "1")return;
        if(window.gameType == "lose")return;
        var self = this;
        if(this.gameWin == null)
        {
            this.onLodeAudioRes("win",(audio)=>{
                self.gameWin = audio;
                cc.audioEngine.play(self.gameWin);
            });
        }
        else cc.audioEngine.play(this.gameWin);
    },

    //游戏失败音效
    playGameLoseAudio:function(){
        if(window.gameConfig.isOpenSound != "1")return;
        if(window.gameType == "win")return;
        var self = this;
        if(this.gameLose == null)
        {
            this.onLodeAudioRes("lose",(audio)=>{
                self.gameLose = audio;
                cc.audioEngine.play(self.gameLose);
            });
        }
        else cc.audioEngine.play(this.gameLose);
    },

    //中球音效
    playQiuPassAudio:function(){
        if(window.gameConfig.isOpenSound != "1")return;
        if(window.gameType == "win" && window.gameType == "lose")return;
        var self = this;
        if(this.qiuPass == null)
        {
            this.onLodeAudioRes("qiuPass",(audio)=>{
                self.qiuPass = audio;
                cc.audioEngine.play(self.qiuPass);
            });
        }
        else cc.audioEngine.play(this.qiuPass);
    },

    //球碰撞音效
    playQiuColliderAudio:function(){
        if(window.gameConfig.isOpenSound != "1")return;
        if(window.gameType != "play")return;
        var self = this;
        let nowTime =  new Date();
        let timeCha = (nowTime - this.time) / 1000;
        if(timeCha < 0.15)return;
        if(this.qiuCollider == null)
        {
            this.time = nowTime;
            this.onLodeAudioRes("qiuCollider",(audio)=>{
                self.qiuCollider = audio;
                cc.audioEngine.play(self.qiuCollider);
            });
        }
        else cc.audioEngine.play(this.qiuCollider);
    },

    //游戏背景音效
    playGameMusic:function(type){        
        // if(window.gameConfig.isOpenMusic != 1)return;
        var self = this;
        if(this.gameMusic == null)
        {
            this.onLodeAudioRes("bjMusic",(audio)=>{
                self.gameMusic = audio;
                if(!type)return
                self.gameMusicId = cc.audioEngine.play(self.gameMusic);
                cc.audioEngine.setLoop(self.gameMusicId, true);
            });
        }
        else 
        {
            if(type)
            {
                if(cc.audioEngine.isLoop(this.gameMusicId))return;
                this.gameMusicId = cc.audioEngine.play(this.gameMusic);
                cc.audioEngine.setLoop(self.gameMusicId, true);
            }
            else if(!type)
            {
                cc.audioEngine.stop(this.gameMusicId)
            }
        }
    },

    onLodeAudioRes:function(audioName, callback){
        cc.loader.loadRes("sound/" + audioName, cc.audioEnging, function (err, audio) {
            if(err == null)callback(audio)
        });
    },
});

module.exports = audioMgr;
